package com.nny.alarm.edit;

import java.util.Calendar;

import android.content.Context;
import android.net.Uri;

import com.umeng.analytics.MobclickAgent;
import com.uxwine.alarm.alert.RingManager;
import com.nny.alarm.R;
import com.nny.alarm.Settings;
import com.nny.alarm.audio.AudioEffect;
import com.nny.alarm.audio.RingPlayer;
import com.nny.alarm.more.AlarmBriefUtil;
import com.nny.alarm.widget.DialView;
import com.nny.alarm.widget.DigitalView;
import com.nny.alarm.widget.TimeView;
import com.nny.alarm.widget.TimeView.OnDigitalClickedListener;
import com.uxwine.alarm.Alarm;
import com.uxwine.alarm.AlarmCore;
import com.uxwine.alarm.TimeCycle2;
import com.uxwine.alarm.TimeRecord;
import com.uxwine.util.UxToast;

public class AlarmEditor implements DialView.OnDialListener, OnDigitalClickedListener
{
	Context mCtx;
	Alarm mAlarm, mBkAlarm;
	TimeView mDigital;
	DialView mDial;
	RingPlayer mRing;
	IAlarmEditor[] mEditors;
	
	int mnStep = 0;
	
	public static final int STEP_CYCLE 	= 0;
	public static final int STEP_TIME	= 1;
	public static final int STEP_RING	= 2;
	
	public AlarmEditor(Context ctx)
	{
		mCtx = ctx;
		mRing = new RingPlayer(ctx);
		mEditors = new IAlarmEditor[3];
		mEditors[STEP_CYCLE] = new CycleEditor(this);
		mEditors[STEP_TIME] = new TimeEditor(this);
		mEditors[STEP_RING] = new RingEditor(this);
	}
	
	public void init(TimeView dttv, DialView dtv)
	{
		mDigital = dttv;
		mDial = dtv;
		mDial.setOnDialListener(this);
		mDigital.setOnDigitalClickedListener(this);
	}
	
	public void eidtor(Alarm alarm)
	{
		if(alarm == null)
			create();
		else
			mAlarm = alarm;
		
		mBkAlarm = new Alarm();
		mBkAlarm.copy(mAlarm);
		
		step(STEP_CYCLE);
	}
	
	public void save()
	{
		Alarm a = build();
		long time = -1;
		if(a.id == -1)
		{
			mAlarm.label = mCtx.getString(R.string.label_edit_tv_default);
			time = AlarmCore.addAlarm(mCtx, a);
			mAlarm.id = a.id;
		}
		else
		{
			time = AlarmCore.setAlarm(mCtx, a);
		}
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(time);
		
		final AlarmBriefUtil abu = AlarmBriefUtil.getInstance(mCtx);
		UxToast.sshow(mCtx, mCtx.getString(R.string.edit_nextalert_tip, abu.getTimeString(a)));
		
		String alert = mAlarm.alert == null ? "" : mAlarm.alert.toString();
		new Settings.Ring(mCtx).set(Settings.Ring.KEY_DEFUAL_ALERT, alert);
		

		MobclickAgent.onEvent(mCtx, "editor_save", "alarm:" + a.id + ",alert:" + alert);
	}
	
	public Alarm build()
	{
		Alarm a = new Alarm();
		mAlarm.enabled = true;
		mAlarm.trigger = 0;
		if(mAlarm.cycle.isOnce())
		{
			final TimeRecord t = mAlarm.time;
			Calendar c = Calendar.getInstance();
			c.set(t.get(TimeRecord.YEAR), 
					t.get(TimeRecord.MONTH),
					t.get(TimeRecord.DATE), 
					t.get(TimeRecord.HOUR), 
					t.get(TimeRecord.MINUTE));
	        c.set(Calendar.SECOND, 0);
	        c.set(Calendar.MILLISECOND, 0);
			mAlarm.trigger = c.getTimeInMillis();
		}
		if(mAlarm.alert == null)
			mAlarm.alert = RingManager.getDefaultRing();
		
		a.copy(mAlarm);
		return a;
	}
	
	public void create()
	{
		String alert = new Settings.Ring(mCtx).getString(Settings.Ring.KEY_DEFUAL_ALERT, null);
		if(null != alert)
			mAlarm = new Alarm(Uri.parse(alert));
		else
			mAlarm = new Alarm(RingManager.getDefaultRing());
	}
	
	public void step(int step)
	{
		mnStep = step;
		mEditors[step].edit();
	}
	
	public void over()
	{
		mEditors[mnStep].over();
	}
	
	public void reset()
	{
		for(IAlarmEditor e : mEditors)
			e.reset();
	}

	@Override
	public boolean onMarkChanged(int index)
	{
		mEditors[mnStep].onMarkChange(index);
		AudioEffect.getInstance(mCtx).play(AudioEffect.MARK);
		return true;
	}

	@Override
	public boolean onMarked(int index)
	{
		mEditors[mnStep].onMarked(index);
		return true;
	}

	@Override
	public void onNavClicked()
	{
		
		if(!mEditors[mnStep].next())
			UxToast.sshow(mCtx, R.string.edit_alarm_over);
		
		AudioEffect.getInstance(mCtx).play(AudioEffect.BUTTON);

		MobclickAgent.onEvent(mCtx, "editor_nav", "step:" + mnStep);
	}

	@Override
	public void onDigitalClicked(DigitalView dv, int type) 
	{
		if(dv.isEnabled())
		{
			over();
			if(mnStep != STEP_TIME)
			{
				step(STEP_TIME);
			}
			
			TimeEditor te = (TimeEditor)mEditors[STEP_TIME];
			te.edit(type);
			
			MobclickAgent.onEvent(mCtx, "editor_digital", "step:" + type);
		}		
	}
}
